package com.example.jdx_no_2;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Timer;
import java.util.TimerTask;

public class 测试进度条 extends AppCompatActivity {
    private Button backbutton2;
    private ProgressBar progressBar;
    private Button btn;
    private Handler handler;
    private int i = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uc2);
        progressBar=findViewById(R.id.progressBar);
        btn=findViewById(R.id.btn);
        backbutton2=findViewById(R.id.back);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler=new Handler(){
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        if (msg.what == 0x122) {
                            if (i <= 100) {
                                progressBar.setProgress(i);
                                i++;
                            }
                        }
                    }
                };
                new Timer().schedule(new TimerTask() {

                    public void run() {
                        handler.sendEmptyMessage(0x122);
                    }
                }, 0, 100);
            }
        });
        backbutton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(测试进度条.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }
}
